--################# Header
include("weapons/gmod_tool/stargate_base_tool.lua");
TOOL.Category="Janus's Lantis Tech";
TOOL.Name="#ZPM Spawner";

TOOL.ClientConVar["autoweld"] = 1;
-- The default model for the GhostPreview
TOOL.ClientConVar["model"] = "models/zpmnusson_teleporter_off.mdl";

-- Information about the SENT to spawn
TOOL.Entity.Class = "zpm_spawner";
TOOL.Entity.Limit = 1;

-- Add the topic texts, you see in the upper left corner
TOOL.Topic["name"] = "ZPM Spawner";
TOOL.Topic["desc"] = "Creates a Zero Point Module Spawner";
TOOL.Topic[0] = "Left click, to spawn the ZPM Spawner";
-- Adds additional "language" - To the end of these files, the string "_*classname*" will be added, using TOOL.Entity["class"]. 
-- E.g. TOOL.Language["Undone"] will add the language "Undone_prop_physics" when TOOL.Entity["class"] is "prop_physics"
TOOL.Language["Undone"] = "ZPM Spawner removed";
TOOL.Language["Cleanup"] = "ZPM Spawner";
TOOL.Language["Cleaned"] = "Removed all ZPM Spawners";
TOOL.Language["SBoxLimit"] = "Hit the ZPM Spawner limit";
--################# Code

--################# LeftClick Toolaction @aVoN
function TOOL:LeftClick(t)
	if(not StarGate.HasResourceDistribution and not StarGate.HasWire) then return false end;
	if(t.Entity and t.Entity:IsPlayer()) then return false end;
	if(t.Entity and t.Entity:GetClass() == self.Entity.Class) then return false end;
	if(CLIENT) then return true end;
	if(not self:CheckLimit()) then return false end;
	local p = self:GetOwner();
	--######## Spawn SENT
	local e = self:SpawnSENT(p,t);
	--######## Weld things?
	local c = self:Weld(e,t.Entity,util.tobool(self:GetClientNumber("autoweld")));
	--######## Cleanup and undo register
	if not file.Exists("../models/magnusson_teleporter_off.mdl") then
		AlertPlayer(p, "Error: HL2:EP2 Not installed or enabled" );
		AlertPlayer(p, "So Model has no sound or effects." );
	end
	e.Player = p;
	
	self:AddUndo(p,e,c);
	self:AddCleanup(p,c,e);
	return true;
end

--################# Controlpanel @aVoN
function TOOL:ControlsPanel(Panel)
	if(StarGate.HasResourceDistribution and StarGate.HasWire) then
		Panel:CheckBox("Autoweld","zpm2_autoweld");
		Panel:Help("This is a ZPM Spawner.\n It can bypass the ZPM limit! Have a look in the MckaysPack.lua file to see the max vale.");
	else
		Panel:Help("The Zero Point Module Mk2 (and Spawner) perfectly fits into Resource Distribution, LifeSupport and Wire.\nYou seem not to have all these installed.\nSo the ZPM Mk2 is 'quite useless for you' :)");
		--Dont you just love aVoN's england!
	end
end

--################# Register Stargate hooks. Needs to be called after all functions are loaded!
TOOL:Register();